package com.ljy.my_study.json转对象;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.ljy.my_study.json转对象.util.Json2ClassBuilder;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.*;

/**
 * @author lijunying
 * @date 2020/7/6 13:41
 */
public class JsonBuilder {

    public static void main(String[] args) throws IOException {
        String fileName="C:\\Users\\lijunying.UT\\Desktop\\gatherPayRequest.txt";
        BufferedReader br=new BufferedReader(new FileReader(fileName));
        List<List<String>> lineArrList=new ArrayList<>();
        String line;
        while((line=br.readLine())!=null){
            List<String> list= Arrays.asList(line.split("\t"));
            if(list.size()<=0){
                list=new ArrayList<>();
                list.add("");
            }
            lineArrList.add(list);
        }
        System.out.println(lineArrList);
        int level=1;
        Map<String,Object> map=new LinkedHashMap<>();
        setValue(map,lineArrList,0,level);
        System.out.println("---------------");

        ObjectMapper objectMapper=new ObjectMapper();
        Map<String,Object> newMap=objectMapper.readValue(objectMapper.writeValueAsString(map), new
                TypeReference<Map<String,Object>>() {});
        Json2ClassBuilder.createBeanFile("PayRequest",(LinkedHashMap<String,Object>)newMap,
                "com.ut","C:\\Users\\lijunying.UT\\Desktop\\params\\class");

    }

    private static int setValue(Map<String,Object> rootMap,List<List<String>> lineArrList,int
            index,int level){
        for(;index<lineArrList.size();index++){
            List<String> lineArr=lineArrList.get(index);
            String paramName=lineArr.get(0);
            String type=null;
            if(lineArr.size()>3){
                type=lineArr.get(2);
            }else{
                type="object";
            }
            type=type.toLowerCase();
            String[] arr=paramName.split("\\.\\.");
            if(arr.length==1){
                paramName=arr[0];
            }else{
                paramName=arr[arr.length-1];
            }
            if("Orderlist".equals(paramName)){
                System.out.println(paramName);
            }
            if(paramName.isEmpty()){
                continue;
            }
            Object value;
            switch (type){
                case "varchar2":value="string";break;
                case "number":{
                    if(lineArr.get(3).contains(",")){
                        value=1.1D;
                    }else{
                        value=1;
                    }
                    break;
                }
                case "char":value="string";break;
                default:value="string";
            }

            paramName=changeParamName(paramName);
            if(index+1<lineArrList.size()){
                String nextParamName=nextParamName(lineArrList,index+1);
                int nextLen=nextParamName.split("\\.\\.").length;
                if(arr.length<nextLen){
                    Map<String,Object> map= new LinkedHashMap<>();
                    index=setValue(map,lineArrList,index+1,level+1);
                    rootMap.put(paramName,map);
                }else{
                    value+="->"+getDesc(lineArrList,index);
                    rootMap.put(paramName,value);
                }
                if(arr.length>nextLen){
                    return index;
                }
            }else{
                value+="->"+getDesc(lineArrList,index);
                rootMap.put(paramName,value);
            }


        }
        return index;
    }

    private static String nextParamName(List<List<String>> lineArrList,int index){
        String nextParamName=null;
        while(true){
            if(index>=lineArrList.size()){
                return nextParamName;
            }
            if(lineArrList.get(index).size()<=0){
                index++;
                continue;
            }
            nextParamName=lineArrList.get(index).get(0);
            if(nextParamName.equals("")){
                index++;
                continue;
            }
            return nextParamName;
        }
    }

    private static String getDesc(List<List<String>> lineArrList,int index){
        StringBuilder sb=new StringBuilder();
        int start=index;
        for(;index<lineArrList.size();index++){
            List<String> lineArr=lineArrList.get(index);
            if(lineArrList.get(index).size()<=0){
                continue;
            }
            if(lineArrList.get(index).get(0).equals("")||index==start){
                if(!lineArr.get(1).equals("")){
                    sb.append(lineArr.get(1)+"\n");
                }
                if(lineArr.size()>4&&!lineArr.get(4).equals("")){
                    sb.append(lineArr.get(4)+"\n");
                }
                if(lineArr.size()>5&&!lineArr.get(5).equals("")){
                    sb.append(lineArr.get(5)+"\n");
                }
            }else{
                break;
            }
        }
        return sb.toString();
    }

    private static String changeParamName(String paramName){
        String[] arr=paramName.split("_");
        StringBuilder sb=new StringBuilder();
        for(int i=0;i<arr.length;i++){
            if(i==0){
                arr[i]=arr[i].toLowerCase();
            }
            sb.append(arr[i]);
        }
        return sb.toString();
    }
}
